package com.csc.dao;

import java.util.List;







import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.csc.controller.EntityManagerFactoryUtil;
import com.csc.domain.User;

@Repository(value="UserDao")
public class UserDaoImpl implements UserDao {

	static final Logger logger=Logger.getLogger(UserDaoImpl.class);
	public List<User> getAllUsers() {
		// TODO Auto-generated method stub
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();
/*@Author Thanh Binh*/
		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		logger.info("Go getAllUsers");
		List<User> listUser = null;
		try {
			entr.begin();
		TypedQuery<User> query = entityManager.createQuery(
		        "SELECT user FROM User user ", User.class);
		
			listUser = query.getResultList();
		logger.info("Get All Account"  +  listUser );
		entr.commit();
		} catch (Exception e) {
			logger.info("get Transaction Error");
			entityManager.close();
		}
			return listUser;
	}
		
	/*public User checkLogin(String userName,String pass){
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();
		
		// -----------Begin transaction-----------
		try {
			entr.begin();
			User user = entityManager.find(User.class,userName);
			if(null!=user && user.getPassword().equals(pass)){
				return user;
			}
			entr.commit();
		} catch (Exception e) {
			// TODO: handle exception
			entityManager.close();
		}

	}*/
	
	

}
